<?php 
class CRequestHandler 
{
	private static $_instance;
	 
	 private $_req;
	 private $_session;
	 private $_user;
	
	private function __construct() 
    { 
      $this->_req=new CHttpRequest();
	  $this->_session=new CHttpSession();
	  $this->_user=new CWebUser();
	  $this->_user->setRequest($this->_req);
	  $this->_user->setSession($this->_session);
	  $this->_user->init();
    } 
	
    public static function instance() 
    { 
        if (!self::$_instance) 
        { 
            self::$_instance = new CRequestHandler(); 
        } 
 
        return self::$_instance; 
    } 
		
	public function getRequestURI()
	{
		return $this->_req->getRequestUri();
	}
	
	public function getRequest()
	{
		return $this->_req;
	}
	
	public function getSession()
	{
		return $this->_session;
	}
	
	public function getUser()
	{
		return $this->_user;
	}
	
	public function dispatchRequest($route,&$data)
	{
		$data->add('CurrentRequest',$this);
		$data->add('CurrentUser',$this->_user);
		$data->add('CurrentRoute',$route);
		$methodName=$route->getMethod();
		$controllerPath=$route->getControllerPath();
		$controllerName=$route->getController();
		if(is_string($controllerPath)&&is_string($controllerName))
		{
			Deepr::autoloadByFullPath($controllerPath.DIRECTORY_SEPARATOR.$controllerName.'.php');
			$controller =Deepr::createInstance($controllerName,$data);
			$tpl=Deepr::createInstance('CTemplator',$data);
			$controller->init($tpl);
			$controller->$methodName();
			unset($controller);
			unset($tpl);			
		}
		$data->clear();
		unset($data);	
	}
}	